package org.six.background.service.impl;

import org.six.background.mapper.ShopMapper;
import org.six.background.pojo.Shop;
import org.six.background.pojo.ShopExample;
import org.six.background.service.ShopService;
import org.six.background.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
public class ShopServiceImpl implements ShopService {
    @Autowired(required = false)
    private ShopMapper shopMapper;
    @Autowired
    private UploadUtil uploadUtil;
    private String dirname="/shops";

    //查询商品
    @Override
    public List<Shop> shopshow() {
        return shopMapper.selectByExample(null);
    }

    @Override
    public String upload(MultipartFile file, HttpServletRequest req) {
        String url=uploadUtil.upload(file,req,dirname);
        System.out.println(url);
        return url;
    }

    //增加商品
    @Override
    public int shopadd(Shop shop) {
        if(shop.getShopname()!=""){
            int i = shopMapper.insertSelective(shop);
            return i;
        }else {
            return 0;
        }
    }
    //删除商品
    @Override
    public String shopdelete(int shopid) {
        System.out.println(shopid);
            shopMapper.deleteByPrimaryKey(shopid);
            return "ok";


    }
    //修改商品
    @Override
    public int shoprevise(Shop shop) {
        ShopExample shopExample=new ShopExample();

        ShopExample.Criteria c = shopExample.createCriteria();
        c.andShopidEqualTo(shop.getShopid());

        int i = shopMapper.updateByPrimaryKeySelective(shop);
        return i;

    }
}
